package com.health.adapter;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Locale;

import com.health.R;
import com.health.tablebean.ARCHIVE_BASEINFO;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

public class TongjiAdapter extends BaseAdapter {

	private List<ARCHIVE_BASEINFO> infoList;
	private Context context;
	
	public TongjiAdapter(Context context,List<ARCHIVE_BASEINFO> infoList) {
		super();
		this.context = context;
		this.infoList = infoList;
	}
	
	

	public void setInfoList(List<ARCHIVE_BASEINFO> infoList) {
		this.infoList = infoList;
	}



	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return infoList.size();
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return infoList.get(position);
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolder holder;
		if (convertView ==null) {
			convertView = View.inflate(context, R.layout.tongji_list_item, null);
			holder = new ViewHolder();
			holder.nameTv = (TextView) convertView.findViewById(R.id.tongji_list_name);
			holder.sexTv = (TextView) convertView.findViewById(R.id.tongji_list_sex);
			holder.nationTv = (TextView) convertView.findViewById(R.id.tongji_list_nation);
			holder.birthdayTv = (TextView) convertView.findViewById(R.id.tongji_list_birthday);
			holder.phoneTv = (TextView) convertView.findViewById(R.id.tongji_list_phone);
			holder.idTv = (TextView) convertView.findViewById(R.id.tongji_list_idcard);
			holder.createDateTv = (TextView) convertView.findViewById(R.id.tongji_list_createdate);
			holder.updateDateTv = (TextView) convertView.findViewById(R.id.tongji_list_updatadate);
			holder.examDataTv = (TextView) convertView.findViewById(R.id.tongji_list_examdate);
			convertView.setTag(holder);
			
		}else {
			holder = (ViewHolder) convertView.getTag();
		}
		
		ARCHIVE_BASEINFO info = infoList.get(position);
		holder.nameTv.setText(info.getCUSTOMERNAME());
		holder.sexTv.setText(info.getSEX().equals("DM01-08_1")?"男":"女");
		holder.nationTv.setText(info.getMINORITY());
		holder.birthdayTv.setText(fromDate2Str(info.getBIRTHDAY()));
		holder.phoneTv.setText(info.getPHONE());
		holder.idTv.setText(info.getIDCARD());
		holder.createDateTv.setText(fromDate2Str(info.getCREATED_DATE()));
		holder.updateDateTv.setText(fromDate2Str(info.getLAST_UPDATE_DATE()));
		holder.examDataTv.setText("");
		if (position%2==0) {
			convertView.setBackgroundColor(context.getResources().getColor(R.color.table_row_odd));
		}else {
			convertView.setBackgroundColor(context.getResources().getColor(R.color.white));
		}
		
		
		return convertView;
	}
	
	private String fromDate2Str(Date date){
		if (date== null) {
			return "";
		}
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd",Locale.CHINA);
		return format.format(date);
	}
	

	private class ViewHolder{
		
		TextView nameTv;
		TextView sexTv;
		TextView nationTv;
		TextView birthdayTv;
		TextView phoneTv;
		TextView idTv;
		TextView createDateTv;
		TextView updateDateTv;
		TextView examDataTv;
		
		
	}

}
